﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using Model;

namespace TanHungLong
{
    public partial class FrmDoiMatKhau : RmsControl.FrmBase
    {
        #region variant
        private FrmMain main = null;
        #endregion variant

        //=====================================
        #region form
        public FrmDoiMatKhau(FrmMain main)
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.main = main;
            this.main.ShowBgr();
            ShowForm(this.main);

            this.Shown += new EventHandler(FrmDoiMatKhau_Shown);
            this.FormClosing += new FormClosingEventHandler(FrmDoiMatKhau_FormClosing);

            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.chkHienMatKhau.CheckedChanged += new EventHandler(chkHienMatKhau_CheckedChanged);

            this.KeyDown += new KeyEventHandler(FrmDoiMatKhau_KeyDown);
        }

        private void FrmDoiMatKhau_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();
            else
                if (this.btnSave.Visible && ((e.KeyCode == Keys.S && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F6)))
                    Save();
                else
                    if (this.btnClose.Visible && ((e.KeyCode == Keys.H && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F12)))
                        this.Close();
        }

        private void FrmDoiMatKhau_Shown(object sender, EventArgs e)
        {
            this.txtMatKhau.Focus();
        }

        private void FrmDoiMatKhau_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
            else
            {
                this.main.Controls.Remove(this);
                this.main.HideBgr();
                this.Dispose();
            }
        }
        #endregion form

        //=====================================
        #region control
        private void chkHienMatKhau_CheckedChanged(object sender, EventArgs e)
        {
            this.txtNhapLaiMatKhau.Enabled = !this.chkHienMatKhau.Checked;
            this.txtNhapLaiMatKhau.Properties.PasswordChar = (this.chkHienMatKhau.Checked ? ' ' : '●');
        }
        #endregion control

        //=====================================
        #region button
        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        #endregion grid

        //=====================================
        #region function
        private void Save()
        {
            this.dxErrorProvider1.SetError(this.txtMatKhau, "");
            this.dxErrorProvider1.SetError(this.txtMatKhauMoi, "");
            this.dxErrorProvider1.SetError(this.txtNhapLaiMatKhau, "");

            ClTripleDES tripleDes = new ClTripleDES();
            string MatKhauCu = "";

            if (Session.LoginID == -1)
                MatKhauCu = tripleDes.Decrypte(Session.thietDatBOCollection.GetStrValue(LoaiThietDat.MatKhau));
            else
            {
                NhanVienDangNhapBO nhanVienBO = NhanVienDangNhapBO.GetRootNhanVienDangNhapBO(Session.LoginID);
                MatKhauCu = tripleDes.Decrypte(nhanVienBO.MatKhau);
            }

            if (this.txtMatKhau.Text != MatKhauCu)
            {
                this.dxErrorProvider1.SetError(this.txtMatKhau, "Mật khẩu không đúng");
                return;
            }

            if (this.txtMatKhauMoi.Text.Length == 0)
            {
                this.dxErrorProvider1.SetError(this.txtMatKhauMoi, "Chưa nhập mật khẩu mới");
                return;
            }

            if (this.chkHienMatKhau.Checked == false)
                if (this.txtMatKhauMoi.Text != this.txtNhapLaiMatKhau.Text)
                {
                    this.dxErrorProvider1.SetError(this.txtNhapLaiMatKhau, "Mật khẩu không khớp với mật khẩu mới");
                    return;
                }

            if (Session.LoginID == -1)
            {
                Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.MatKhau, tripleDes.Encrypte(this.txtMatKhauMoi.Text));
                Session.thietDatBOCollection = Session.thietDatBOCollection.Save();
            }
            else
            {
                NhanVienDangNhapBO nhanVienBO = NhanVienDangNhapBO.GetRootNhanVienDangNhapBO(Session.LoginID);
                nhanVienBO.MatKhau = this.txtMatKhauMoi.Text;
                nhanVienBO = nhanVienBO.Save();
            }

            this.Close();
        }
        #endregion function
    }
}